package com.sky.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

/**
 * &#064;Description  TODO
 * &#064;Date  2023-07-08 16:30
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override

    public void add(CategoryDTO categoryDTO) {
        //获取添加人
        //Long currentId = BaseContext.getCurrentId();
        //创建菜品实体类
        Category category = new Category();
        //将实体类categoryDTO的属性拷贝给实体类category
        BeanUtils.copyProperties(categoryDTO,category);
        //修改时间和创建时间
        //category.setCreateTime(LocalDateTime.now());
        //category.setUpdateTime(LocalDateTime.now());
        //修改人和创建人
        //category.setCreateUser(currentId);
        //category.setUpdateUser(currentId);
        //启用禁用状态
        category.setStatus(StatusConstant.ENABLE);
       categoryMapper.add(category);
    }

    /**
     * 根据id删除
     * @param id
     */
    @Override
    public void delete(Long id) {
        //查询当前分类是否关联了菜品,如果关联了抛出异常
        Integer integer = dishMapper.countByCategoryId(id);
        if ( integer>0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        //查询当前分类是否关联了套餐,如果关联了抛出异常
        Integer integer1 = setmealMapper.countByCategoryId(id);
        if (integer1>0){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        categoryMapper.dlete(id);
    }

    /**
     * 分页查询
     * @param categoryDTO
     * @return
     */
    @Override
    public PageResult page(CategoryPageQueryDTO categoryDTO) {
        //使用插件 categoryDTO.getPage()页码,categoryDTO.getPageSize()每页显示数量
        PageHelper.startPage(categoryDTO.getPage(),categoryDTO.getPageSize());
        //插件查询到的返回数据
        Page<Category> results= categoryMapper.page(categoryDTO.getName(),categoryDTO.getType());
        long total = results.getTotal();//总条数
        List<Category> result = results.getResult();//当前数据集合
        return new PageResult(total,result);
    }

    /**
     * 启用禁用接口
     * @param status
     * @param id
     */
    @Override
    public void status(Integer status, Long id) {
        //获取修改人的id
        Long currentId = BaseContext.getCurrentId();
        //通过链式编程将参数传递进去
        Category build = Category.builder()
                .id(id)
                .status(status)
                .updateTime(LocalDateTime.now())
                .updateUser(currentId)
                .build();
        categoryMapper.update(build);
    }

    /**
     * 根据类型查询接口
     * @return
     */
    @Override
    public List<Category> list(Integer type) {
         List<Category> categories= categoryMapper.list(type);
        return categories;
    }

    /**
     * 修改分页接口
     * @param categoryDTO
     */
    @Override

    public void update(CategoryDTO categoryDTO) {
        //创建菜品分类的实体类对象
        Category category = new Category();
        //获取修改人id
       // Long currentId = BaseContext.getCurrentId();
        //通过BeanUtils的api将categoryDTO的参数拷贝给category
        BeanUtils.copyProperties(categoryDTO,category);
        //更新修改人和修改时间
       // category.setUpdateUser(currentId);
        //category.setUpdateTime(LocalDateTime.now());
        categoryMapper.update(category);
    }
}
